'''

Case Type： pid文件
Case Name： pid文件修改后停止数据库
Create At:  2020/06/28
Owner    :  n@ningyali
Descption:  1.修改pid文件为空，停止数据库 2.修改文件进程号，停止数据库 3.修改data目录位置，停止数据库 4.修改监听端口号，停止数据库 5.修改socket位置，停止数据库

'''

import sys
import os
import unittest
import time
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *


class PidFileModifiedStop(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.log = Logger()
        cls.log.info('------------Opengauss_Reliability_Dbsys_Case079.py start------------')
        cls.pid_file_path = os.path.join(macro.DB_INSTANCE_PATH, macro.PID_FILE_NAME)
        cls.rootNode = Node()
        cls.dbUserNode = Node(node='dbuser')
        cls.sh = CommonSH()
        cls.com = Common()
        cls.Constant = Constant()

    def setUp(self):
        self.log.info('----------------------------初始化-----------------------------')
        self.log.info('----------------------------检查数据库状态-----------------------------')
        db_status = self.sh.getDbClusterStatus('status')
        if not db_status:
            self.sh.startDbCluster()

            db_status = self.sh.getDbClusterStatus('status')
            if not db_status:
                raise Exception("db status is not true, please check!")
        self.log.info(db_status)

    #  修改pid文件为空，停止数据库
    def test_1_empty_pid_file(self):
        self.log.info('----------------------------test1:修改pid文件为空，停止数据库-----------------------------')
        # 修改pid文件为空
        empty_cmd = f'> {self.pid_file_path}'
        self.log.info(empty_cmd)
        empty_msg = self.dbUserNode.sh(empty_cmd).result()
        self.log.info(empty_msg)
        self.assertEqual(empty_msg, '')

        # 默认stop关闭数据库连接，无法关闭
        stop_msg = self.sh.executeGsctl('stop', self.Constant.GS_CTL_STOP_SUCCESS_MSG)
        self.log.info(stop_msg)
        self.assertFalse(stop_msg)

        # immediate模式强制关闭，可关闭
        im_stop_msg = self.sh.executeGsctl('stop', self.Constant.GS_CTL_STOP_SUCCESS_MSG,
                                           '-m immediate')
        self.log.info(im_stop_msg)
        self.assertTrue(im_stop_msg)

    #  修改文件进程号，停止数据库
    def test_2_modified_pid(self):
        self.log.info('----------------------------test2:修改文件进程号，停止数据库-----------------------------')
        # 修改pid文件进程号
        mod_pid_cmd = f"sed -i '1,1c test' {self.pid_file_path};"
        self.log.info(mod_pid_cmd)
        chmod_msg = self.dbUserNode.sh(mod_pid_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual(chmod_msg, '')

        # 默认stop关闭数据库连接，无法关闭
        stop_msg = self.sh.executeGsctl('stop', self.Constant.GS_CTL_STOP_SUCCESS_MSG)
        self.log.info(stop_msg)
        self.assertFalse(stop_msg)

        # immediate模式强制关闭，可关闭
        im_stop_msg = self.sh.executeGsctl('stop', self.Constant.GS_CTL_STOP_SUCCESS_MSG,
                                           '-m immediate')
        self.log.info(im_stop_msg)
        self.assertTrue(im_stop_msg)

    #  修改data目录位置，停止数据库
    def test_3_modified_data_dir(self):
        self.log.info('----------------------------test3:修改data目录位置，停止数据库-----------------------------')
        # 修改data目录位置
        mod_data_dir_cmd = f"sed -i '2,2c /test/test' {self.pid_file_path};"
        self.log.info(mod_data_dir_cmd)
        chmod_msg = self.dbUserNode.sh(mod_data_dir_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual(chmod_msg, '')

        # 默认stop关闭数据库连接，可关闭
        stop_msg = self.sh.executeGsctl('stop', self.Constant.GS_CTL_STOP_SUCCESS_MSG)
        self.log.info(stop_msg)
        self.assertTrue(stop_msg)

        # 启动数据库
        start_msg = self.sh.startDbCluster()

        self.assertTrue(start_msg)

        # 修改data目录位置
        mod_data_dir_cmd = f"sed -i '2,2c /test/test' {self.pid_file_path};"
        self.log.info(mod_data_dir_cmd)
        chmod_msg = self.dbUserNode.sh(mod_data_dir_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual(chmod_msg, '')

        # immediate模式强制关闭，可关闭
        im_stop_msg = self.sh.executeGsctl('stop', self.Constant.GS_CTL_STOP_SUCCESS_MSG,
                                           '-m immediate')
        self.log.info(im_stop_msg)
        self.assertTrue(im_stop_msg)

    #  修改监听端口号，停止数据库
    def test_4_modified_listen_port(self):
        self.log.info('----------------------------test4:修改监听端口号，停止数据库-----------------------------')
        # 修改监听端口号
        mod_listen_port_cmd = f"sed -i '4,4c 11111111111' {self.pid_file_path};"
        self.log.info(mod_listen_port_cmd)
        chmod_msg = self.dbUserNode.sh(mod_listen_port_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual(chmod_msg, '')

        # 默认stop关闭数据库连接，可关闭
        stop_msg = self.sh.executeGsctl('stop', self.Constant.GS_CTL_STOP_SUCCESS_MSG)
        self.log.info(stop_msg)
        self.assertTrue(stop_msg)

        # 启动数据库
        start_msg = self.sh.startDbCluster()

        self.assertTrue(start_msg)

        # 修改监听端口号
        mod_listen_port_cmd = f"sed -i '4,4c 11111111111' {self.pid_file_path};"
        self.log.info(mod_listen_port_cmd)
        chmod_msg = self.dbUserNode.sh(mod_listen_port_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual(chmod_msg, '')

        # immediate模式强制关闭，可关闭
        im_stop_msg = self.sh.executeGsctl('stop', self.Constant.GS_CTL_STOP_SUCCESS_MSG,
                                           '-m immediate')
        self.log.info(im_stop_msg)
        self.assertTrue(im_stop_msg)

    #  修改socket位置，停止数据库
    def test_5_modified_socket_path(self):
        self.log.info('----------------------------test5:修改socket位置，停止数据库-----------------------------')
        # 修改socket位置
        mod_socket_path_cmd = f"sed -i '5,5c /test/test' {self.pid_file_path};"
        self.log.info(mod_socket_path_cmd)
        chmod_msg = self.dbUserNode.sh(mod_socket_path_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual(chmod_msg, '')

        # 默认stop关闭数据库连接，可关闭
        stop_msg = self.sh.executeGsctl('stop', self.Constant.GS_CTL_STOP_SUCCESS_MSG)
        self.log.info(stop_msg)
        self.assertTrue(stop_msg)

        # 启动数据库
        start_msg = self.sh.startDbCluster()

        self.assertTrue(start_msg)

        # 修改socket位置
        mod_socket_path_cmd = f"sed -i '5,5c /test/test' {self.pid_file_path};"
        self.log.info(mod_socket_path_cmd)
        chmod_msg = self.dbUserNode.sh(mod_socket_path_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual(chmod_msg, '')

        # immediate模式强制关闭，可关闭
        im_stop_msg = self.sh.executeGsctl('stop', self.Constant.GS_CTL_STOP_SUCCESS_MSG,
                                           '-m immediate')
        self.log.info(im_stop_msg)
        self.assertTrue(im_stop_msg)

    def tearDown(self):
        msg = self.com.kill_pid(self.rootNode, 9)
        self.assertEqual(msg, '')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('----------------Opengauss_Reliability_Dbsys_Case079:用例执行完毕-----------------')
